package com.cy.pj.sys.service.impl;

import com.cy.pj.common.annotation.RequiredLog;
import com.cy.pj.common.annotation.RequiredTime;
import com.cy.pj.sys.dao.SysLogDao;
import com.cy.pj.sys.pojo.SysLog;
import com.cy.pj.sys.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户日志业务规范的实现
 */
@Transactional(readOnly = false,
        timeout = 60,
        rollbackFor = Throwable.class,
        isolation = Isolation.REPEATABLE_READ)
@Service
public class SysLogServiceImpl implements SysLogService{
    @Autowired
    private SysLogDao sysLogDao;

//    public SysLogServiceImpl(SysLogDao sysLogDao){
//        this.sysLogDao=sysLogDao;
//    }

    @Async
    @Override
    public void saveLog(SysLog sysLog) {
        System.out.println("log.save.thread->"+Thread.currentThread().getName());
        try {
            Thread.sleep(10000);
        }catch (Exception e){}
        sysLogDao.insertLog(sysLog);
    }

    @Transactional
    @RequiredLog(operation = "删除日志")
    @Override
    public int deteById(Long... ids) {
        int rows = sysLogDao.deleteById(ids);
        //这里抛出异常时如何让上面的操作回滚
        //throw new RuntimeException("删除失败");
        return rows;
    }

    @RequiredLog(operation = "查询日志")
    @Override
    public SysLog findById(Long id) {
        SysLog log = sysLogDao.selectById(id);
        return log;
    }

    @Transactional(readOnly = true)
    @RequiredTime
    @RequiredLog(operation = "查询日志")//由此注解描述的方法为日志切入点方法
    @Override
    public List<SysLog> findLogs(SysLog sysLog) {
        System.out.println("log.find.thread->"+Thread.currentThread().getName());
        List<SysLog> list = sysLogDao.selectLogs(sysLog);
        return list;
    }
}
